package com.luoqiu.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoqiu.yygh.cmn.listener.EasyExcelListener;
import com.luoqiu.yygh.cmn.mapper.DictMapper;
import com.luoqiu.yygh.cmn.service.DictService;
import com.luoqiu.yygh.model.cmn.Dict;
import com.luoqiu.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Override
    public Dict selectById(Long id) {
        return getById(id);
    }

    @Override
    @Cacheable(value = "dict", keyGenerator = "keyGenerator")
    public Object findChildData(Long id) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        List<Dict> dicts = baseMapper.selectList(wrapper);
        dicts.forEach(e -> e.setHasChildren(hadChild(e.getId())));
        return dicts;
    }

    @Override
    public void export(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        String fileName = "dict";
        // 表示以下载文件的方式返回
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        List<Dict> dicts = baseMapper.selectList(null);
        List<DictEeVo> data = new ArrayList<>(dicts.size());
        for (Dict dict : dicts) {
            DictEeVo vo = new DictEeVo();
            BeanUtils.copyProperties(vo, dict);
            data.add(vo);
        }
        EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("dict").doWrite(data);
    }

    @Override
    @CacheEvict(value = "dict", allEntries = true) // 清空使用缓存
    public void importData(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), DictEeVo.class, new EasyExcelListener(baseMapper)).sheet().doRead();
    }

    @Override
    public String getName(String dictCode, String value) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        if (dictCode != null) {
            wrapper.eq("dict_code", dictCode);
        }
        wrapper.eq("value", value);
        Dict dict = baseMapper.selectOne(wrapper);
        if (null == dict) return "nil";
        return dict.getName();
    }

    private boolean hadChild(Long id) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        Integer count = baseMapper.selectCount(wrapper);
        return count >= 0;
    }
}
